home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 03 - Advanced Graphics / Example 5 / update.c < prev    next >
Text File  |  1995-02-27  |  2KB  |  114 lines

  1. //
  2. //    File: update.c
  3. //
  4. //    This file contains routines to manage the update from the offscreen to the screen.
  5. //
  6. //    2/19/95 -- Created by Mick
  7. //
  8.  
  9. // include files
  10.  
  11. #include "global.h"
  12.  
  13. #include "update.h"
  14.  
  15. // defines for this file
  16.  
  17. // global function declarations
  18.  
  19. void clearUpdate( void );
  20. void addRectToUpdate( Rect *inUpdateRect );
  21. unsigned char getUpdateRect( Rect *outUpdateRect );
  22.  
  23. // global data owned by this file
  24.  
  25. // local function declarations
  26.  
  27. static void adjustRect( Rect *ioRect );
  28.  
  29. // static data
  30.  
  31. static Rect sDirtyRect;                                // the rect that needs to be udpated
  32. static unsigned char sIsDirty;        // is there a dirty rect?
  33.  
  34. // functions
  35.  
  36.  
  37. //
  38. //    clearUpdate -
  39. //
  40. //    Resets the update to none.
  41. //
  42.  
  43. void clearUpdate( void )
  44. {
  45.     // note that there is no dirty rect
  46.     sIsDirty = kFalse;
  47. }
  48.  
  49.  
  50. //
  51. //    addRectToUpdate -
  52. //
  53. //    Adds a rect to the update area.
  54. //
  55.  
  56. void addRectToUpdate( Rect *inUpdateRect )
  57. {
  58.     // if there is an update rect already
  59.     if( sIsDirty )
  60.         {    
  61.             // add this one to it
  62.             sDirtyRect.top = sDirtyRect.top < inUpdateRect->top ? sDirtyRect.top : inUpdateRect->top;
  63.             sDirtyRect.left = sDirtyRect.left < inUpdateRect->left ? sDirtyRect.left : inUpdateRect->left;
  64.             sDirtyRect.bottom = sDirtyRect.bottom > inUpdateRect->bottom ? sDirtyRect.bottom : inUpdateRect->bottom;
  65.             sDirtyRect.right = sDirtyRect.right > inUpdateRect->right ? sDirtyRect.right : inUpdateRect->right;
  66.             
  67.             // adjust the rect to 32 bit bounds
  68.             adjustRect( &sDirtyRect );
  69.         }
  70.     else
  71.         {
  72.             // make this the dirty rect
  73.             sDirtyRect = *inUpdateRect;
  74.             
  75.             // note that there is a dirty rect
  76.             sIsDirty = kTrue;
  77.             
  78.             // adjust the rect to 32 bit bounds
  79.             adjustRect( &sDirtyRect );
  80.         }
  81. }
  82.  
  83.  
  84. //
  85. //    getUpdateRect -
  86. //
  87. //    Get the dirty rect. If there is one the return is true, otherwise false.
  88. //
  89.  
  90. unsigned char getUpdateRect( Rect *outUpdateRect )
  91. {
  92.     // if there is an update rect, send it out
  93.     if ( sIsDirty )
  94.         {
  95.             *outUpdateRect = sDirtyRect;
  96.         }
  97.  
  98.     // return the dirty flag
  99.     return sIsDirty;
  100. }
  101.  
  102.  
  103. //
  104. //    adjustRect -
  105. //
  106. //    Move the left and right sides to 32 bit boundrys. This will speed up CopyBits.
  107. //
  108.  
  109. void adjustRect( Rect *ioRect )
  110. {
  111.     // expand the rect so its horizontal sides are on 32 bit bounds
  112.     ioRect->left = ioRect->left & 0xfffC;
  113.     ioRect->right = ( ioRect->right + 3 ) & 0xfffC;
  114. }